package main

import (
	"fmt"
	"net"
)

func main() {

	listen, err := net.Listen("tcp", ":8888")
	if err != nil {
		return
	}
	defer listen.Close()

	conn, err := listen.Accept()
	if err != nil {
		return
	}
	defer conn.Close()
	ipAddr := conn.RemoteAddr()
	fmt.Println("ip 地址", ipAddr)
	receMsg := make([]byte, 1024)
	for {
		n, err := conn.Read(receMsg)
		if err != nil {
			return
		}
		result := receMsg[:n]
		fmt.Printf("接收数据来自[ %s]:%s \n", ipAddr, string(result))
		if "exit" == string(result) {
			fmt.Println(ipAddr, "退出连接")
			return
		}
		conn.Write([]byte("服务端数据"))

	}

}
